var addUrl = '';
var editUrl = '';
var deleteUrl = '';
Ext.onReady(function(){
	var listFields = queryScheme.listFields;
	var entityId = queryScheme.entity.id;
	addUrl = WEB_CONTEXT + '/comp/entity?action=preCreate&entityId=' + entityId;
	editUrl = WEB_CONTEXT + '/comp/entity?action=preUpdate&entityId=' + entityId + '&entityObjId=';
	deleteUrl = WEB_CONTEXT + '/comp/entity?action=delete&entityId=' + entityId + '&entityObjId=';
	var cols = new Array();
	for(var i=0; i<listFields.length; i++){
		var col = new Object();
		col.header = listFields[i].entityField.nameCn;
		col.dataIndex = listFields[i].entityField.name;
		col.sortable = true;
		if(!listFields[i].display){
			col.hidden = true;
		}
		cols[cols.length] = col;
	}
	columnDescribe = cols;
	var grid = new Ext.ux.grid.CompSimpleList({
		id:'compGrid',
		renderTo:'grid',
	    iconCls:'icon-grid',
	    layout:'fit',
	    trackMouseOver:true, //鼠标特效   
	    dataUrl:WEB_CONTEXT + '/comp/simpleListData?schemeId=' + queryScheme.id,          // the store load url (required)
		columnDescribe:columnDescribe, //.....(required)
		rowNumber:true,             //true to add a Ext.grid.RowNumberer,defalut(true)
		checkBox:true,           //true to add a Ext.grid.CheckBoxSelectionModel,default(true)
		pagingBar:true,             //true to add a Ext.PagingToolBar,default(true)
		pagingConfig:page,        //config pagingToolBar if pagingBar is true
		qryParams:{},
		width:'100%',
		height:top.Ext.getCmp('tabs').getInnerHeight()-2,
        tbar:[
              {                    
                  text: '新增',
                  cls: 'add',
                  handler: addBomEntity
              },
              '-', 
              {
            	  text: '修改',
                  cls: 'edit',
                  handler: editEntity
              },
              '-', 
              {
            	  text: '删除',
                  cls: 'delete',
                  handler: deleteEntity
              }
             ]
	});
});

/**
 * 新增实体（插件可重写）
 */
var addBomEntity = function(){
    openExtWin(addUrl, refresh, {width:900, height:580});
}

/**
 * 编辑实体
 */
var editEntity = function(){
	var selModel = Ext.getCmp("compGrid").getSelectionModel();
	if(selModel.hasSelection()){
		if(selModel.getCount()>1){
			extAlert("提示","只能选择一条记录进行编辑");
			return;
		}
		var entityObjId = selModel.getSelected().get("OBJ_ID");
		editBomEntity(entityObjId);
	}else{
		extAlert("提示","请选择一条记录进行操作");
	}
}

/**
 * bom对象编辑函数，插件可重写
 * @param {} entityObjId
 */
var editBomEntity = function(entityObjId){
    openExtWin(editUrl + entityObjId, refresh, {width:900, height:580});
}

/**
 * 删除实体
 */
var deleteEntity = function(){
	var selModel = Ext.getCmp("compGrid").getSelectionModel();
	if(selModel.hasSelection()){
		if(selModel.getCount()>1){
			extAlert("提示","只能选择一条记录");
			return;
		}
		var entityObjId = selModel.getSelected().get("OBJ_ID");
		extConfirm("提示","是否删除",function(btn){
			if(btn=="yes"){
				extWait('正在删除数据...');
		        Ext.Ajax.request({
		        	url: deleteUrl + entityObjId,
		        	params: {},
		        	success: function(response, options) {
		        		var responseObj = Ext.util.JSON.decode(response.responseText);                                             
		        		extHide();
		        		extAlert('提示',responseObj.msg,function(){
				 			refresh();
				 		});
		        	}
		        });
			}
		});
	}else{
		extAlert("提示","请选择一条记录进行操作");
	}
}

/**
 * 刷新本页面
 */
function refresh(){
	document.location.reload();
}